/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-09-18     lfz         first version
 */
#include <rtthread.h>
#include "app_ocpp_mgnt_sm.h"
#include "app_ocpp_mgnt_msg.h"
#include "app_ocpp_mgnt_srv.h"

#define LOG_TAG "ocpp.mgnt.srv"
#define LOG_LVL LOG_LVL_INFO
#include "ulog.h"

static void app_ocpp_mgnt_timer_fun(void *parameter) {
    app_ocpp_mgnt_timout_msg_post();
}

static void app_ocpp_mgnt_entry(void *parameter) {
    os_mq_data msg = {0};

    if (!app_ocpp_msg_init()) {
        return;
    }

    if (!app_ocpp_sm_init(app_ocpp_mgnt_timer_fun)) {
        return;
    }

    while (1) {
        memset(&msg, 0, sizeof(os_mq_data));
        if (app_ocpp_msg_recv(&msg, RT_WAITING_FOREVER)) {
            app_ocpp_msg_proc(&msg);
        }
        os_mq_free_mem(&msg);
    }
}

int32_t app_ocpp_mgnt_init(void) {
#define OCPP_MGNT_STACK_SIZE (5120)
#define OCPP_MGNT_PRIORITY (12)

    rt_thread_t tid = RT_NULL;

    tid = rt_thread_create("ocpp_mgnt",
                           app_ocpp_mgnt_entry,
                           RT_NULL,
                           OCPP_MGNT_STACK_SIZE,
                           OCPP_MGNT_PRIORITY, 10);
    if (tid != RT_NULL) {
        rt_thread_startup(tid);
        return RT_EOK;
    }

    return -RT_ERROR;
}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
